﻿// ElementTreeCtrl.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "ElementTreeCtrl.h"


// CElementTreeCtrl

IMPLEMENT_DYNAMIC(CElementTreeCtrl, CTreeCtrl)

CElementTreeCtrl::CElementTreeCtrl()
{
}

CElementTreeCtrl::~CElementTreeCtrl()
{
}


BEGIN_MESSAGE_MAP(CElementTreeCtrl, CTreeCtrl)
	ON_WM_CREATE()
END_MESSAGE_MAP()



// CElementTreeCtrl 消息处理程序

void CElementTreeCtrl::AttachBitmap()
{
	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_CHECK_MODEL_TREE_16))
	{
		m_il.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		SetImageList(&m_il, TVSIL_NORMAL);
	}
}


int CElementTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	AttachBitmap();

	return 0;
}


void CElementTreeCtrl::Populate(BOOL bIsShowAll/*=TRUE*/)
{
	this->DeleteAllItems();
	this->InsertItem("质量元素库", ELEMENT_TREE_ROOT_NODE, ELEMENT_TREE_ROOT_NODE, TVI_ROOT);
	HTREEITEM hRoot = this->GetRootItem();

	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetElements();
	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 code = pElement->GetCode();

			HTREEITEM hItem = this->InsertItem(szName, ELEMENT_TREE_ELEMENT_NODE, ELEMENT_TREE_ELEMENT_NODE, hRoot);
			this->SetItemData(hItem, (DWORD)code);

			PopulateSubElement(code, hItem, bIsShowAll);
		}
		pElements->Release();

		this->Expand(hRoot, TVE_EXPAND);
	}
}

/************************************************************************/
/* TRUE	：如果PopulateAllSubElement添加了子节点                          */
/* FALSE：如果PopulateAllSubElement没有子节点                            */
/************************************************************************/
BOOL CElementTreeCtrl::PopulateSubElement(z_uint32 nParentCode, HTREEITEM hParentItem, BOOL bIsShowAll/*=TRUE*/)
{
	BOOL ret = FALSE;

	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetChildElements(nParentCode);
	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 code = pElement->GetCode();
			HTREEITEM hItem = this->InsertItem(szName, ELEMENT_TREE_SUBE_NODE, ELEMENT_TREE_SUBE_NODE, hParentItem);
			this->SetItemData(hItem, (DWORD)code);

			if (!PopulateItem(code, hItem, bIsShowAll))
			{
				//该模型有检查项，如果Tree处于可编辑状态，需要设置CheckBox
				if (m_isEditable)
				{
					this->SetCheck(hItem, TRUE);
				}
				ret = TRUE;
			}
			else
			{
				if (!bIsShowAll)
				{
					this->DeleteItem(hItem);
				}
			}

		}
		pElements->Release();

		this->Expand(hParentItem, TVE_EXPAND);
	}

	return ret;
}


BOOL CElementTreeCtrl::PopulateItem(z_uint32 nParentCode, HTREEITEM hParentItem, BOOL bIsShowAll/*=TRUE*/)
{
	BOOL ret = FALSE;

	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();

	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetChildElements(nParentCode);

	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 nItemCode = pElement->GetCode();
			//z_uint32 nModelCode = m_pModel->GetID();

			HTREEITEM hItem = this->InsertItem(szName, ELEMENT_TREE_ITEM_NODE, ELEMENT_TREE_ITEM_NODE, hParentItem);
			this->SetItemData(hItem, (DWORD)nItemCode);

			//if (pModelManager->HasItem(nModelCode, nItemCode))
			//{
			//	ret = TRUE;
			//	HTREEITEM hItem = this->InsertItem(szName, ELEMENT_TREE_ITEM_NODE, ELEMENT_TREE_ITEM_NODE, hParentItem);
			//	this->SetItemData(hItem, (DWORD)nItemCode);

			//	if (m_isEditable)
			//	{
			//		this->SetCheck(hItem, TRUE);
			//	}
			//}
			//else
			//{
			//	if (bIsShowAll)
			//	{
			//		//如果全部显示的话，需要把其他的CheckItem也添加到Tree上
			//		HTREEITEM hItem = this->InsertItem(szName, ELEMENT_TREE_ITEM_NODE, ELEMENT_TREE_ITEM_NODE, hParentItem);
			//		this->SetItemData(hItem, (DWORD)nItemCode);
			//	}
			//}

		}
		pElements->Release();

		this->Expand(hParentItem, TVE_EXPAND);
	}

	return ret;
}

void CElementTreeCtrl::SetEditable(BOOL bEditable)
{
	m_isEditable = bEditable;
}

BOOL CElementTreeCtrl::IsEditable()
{
	return m_isEditable;
}